from fastapi import APIRouter,Depends
from app.utils.custom_route import CustomRoute
from app.utils.auth import get_current_user

from app.schema.chat import ChatInfoSchema
from app.schema.base import *
from app.service.chat import ChatService
from app.dao.chat import ChatDao

router = APIRouter(route_class=CustomRoute)

@router.post('/create')
async def create_chat(chat: ChatInfoSchema ,auth_data: dict = Depends(get_current_user)):
    try:
        chat.user_id=auth_data.id
        return await ChatService(auth_data).create_chat(chat, auth_data)
    except BaseException as e:
        print('err-----------',e)
        ChatService(auth_data).dao.db.sess.rollback()
        return  RespBaseSchema(code=50001,message='聊天创建失败')


@router.post('/chatlist')
async def get_chat_list(*, args: ListArgsSchema, auth_data: dict = Depends(get_current_user)):
   try:
      ans = await ChatService(auth_data).get_chat_list(args, auth_data)
      return RespDataSchema(data=ans)
   except BaseException as e:
      return  RespBaseSchema(code=50001,message='获取聊天列表失败')
   

@router.post('/readmesage')
async def read_message(chat_list: List[int], auth_data: dict = Depends(get_current_user)):
    try:
        async for chat in chat_list:
            await ChatService(auth_data).read_message(chat, auth_data)
        return RespBaseSchema()
    except BaseException as e:
        return  RespBaseSchema(code=50001,message='标记已读失败')
    
@router.post('/ailist')
async def get_ai_list(*, args: ListArgsSchema, auth_data: dict = Depends(get_current_user)):
    try:
        return await ChatService(auth_data).get_ai_list(args, auth_data)
    except BaseException as e:
        return RespBaseSchema(code=50001,message='查询失败')